package middleware

import (
	"time"

	"github.com/gin-gonic/gin"
)

const MiddlewareKeyNoCache = "NoCache" // 中间件标识

// NoCache 用于防止浏览器或代理缓存响应
func NoCache(ctx *gin.Context) {
	ctx.Header("Last-Modified", time.Now().UTC().Format(time.RFC1123))
	// 设置多个禁用缓存的 headers
	ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
	ctx.Header("Pragma", "no-cache")
	ctx.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
	// 针对不同浏览器的额外 headers
	ctx.Header("X-Content-Type-Options", "nosniff")
	ctx.Header("X-Frame-Options", "DENY")
	ctx.Header("X-XSS-Protection", "1; mode=block")
	ctx.Next()
}
